package com.sc.sc250601.cook.controller;

import com.sc.sc250601.cook.pojo.Comment1;
import com.sc.sc250601.cook.pojo.Result;
import com.sc.sc250601.cook.service.Comment1Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/comment1")
public class Comment1Controller {

    @Autowired
    private Comment1Service comment1Service;

    @RequestMapping("/getComment")
    public Result getRecipeComment(Integer targetType, Long targetId){
        // 菜谱类型为 0, 动态类型为 1, 课程类型为 2
        List<Comment1> comments = comment1Service.getComments(targetType, targetId);
        return new Result(1, "获取菜谱评论成功", comments);
    }

    @RequestMapping("/addComment")
    public Result addComment(@RequestBody Comment1 comment) {
        // 如果没传就默认 -1
        if(comment.getParentId() == null || comment.getParentId() <= 0){
            comment.setParentId(-1L);
        }
        int i = comment1Service.addComment(comment);
        if (i <= 0) {
            return new Result(0, "添加失败");
        }
        return new Result(1, "添加成功");
    }
}
